<?php
cmspad_import('widget.Widget');
class CheckGroupWidget extends Widget{
	
	public $options = array();
	
	public function addOption($title, $value){
		$this->options[$value] = $title;
	}
	
	public function delOption($value){
		unset($this->options[$value]);
	}
	
	public function query($table, $title, $value, $condition = null){
		$this->options = array();
		$db = cmspad_database();
		$sql = 'SELECT `'.$title.'`,`'.$value.'` FROM `'.$table.'`'.($condition?' WHERE '.$condition:'');
		$rs = $db->query($sql);
		foreach($rs as $r){
			$this->addOption($r[$title], $r[$value]);
		}
	}
	
	public function __toString(){
		$i = 1;
		$html = '';
		foreach($this->options as $k => $v){
			$html .= '<INPUT type="checkbox"';
			if($this->id){
				$html .= ' id="'.$this->id.'_'.$i.'"';
				$i++;
			}
			if($this->name){
				$html .= ' name="'.$this->name.'"';
			}
			if($this->class){
				$html .= ' class="'.$this->class.'"';
			}
			if($this->style){
				$html .= ' style="'.$this->style.'"';
			}
			$html .= ' value="'.$k.'"';
			if($this->value){
				if(is_array($this->value)){
					if(in_array($k, $this->value)){
						$html .= ' checked="checked"';
					}
				}else{
					if($k == $this->value){
						$html .= ' checked="checked"';
					}
				}
			}
			$html .= '/> ';
			$html .= $v;
			$html .= ' ';
		}
		return $html;
	}
}
?>